//
//  Common.m
//  Tograk
//
//  Created by Captain on 12-12-16.
//  Copyright (c) 2012年 Captain. All rights reserved.
//

#import "Common.h"
#import "JSONKit.h"
@implementation Common

+(BOOL)exitsPropertyListFile:(NSString*)fileName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSString *path = [libraryDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.plist",fileName]];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    return [fileManager fileExistsAtPath: path];
}

+(BOOL)createPropertyListFile:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSString *path = [libraryDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.plist",fileName]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSMutableDictionary *data;
    if ([fileManager fileExistsAtPath: path])
        data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    else{
        NSLog(@"我在被创建哦 %@ ",path);
        
        data = [[NSMutableDictionary alloc] init];

//        [data setObject:@"" forKey:@"usernumber"];
    }
    
    return  [data writeToFile: path atomically:YES];
}
+(BOOL)writeKeytoPropertyListFile:(NSString *)fileName value:(id)value forKey:(NSString *)keyName
{
    @try {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *libraryDirectory = [paths objectAtIndex:0];
        NSString *path = [libraryDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.plist",fileName] ];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSMutableDictionary *data;
        if ([fileManager fileExistsAtPath: path])
            data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
        else
            data = [[NSMutableDictionary alloc] init];
        [data setObject:value forKey:keyName];
        return  [data writeToFile: path atomically:YES];
    }
    @catch (NSException *exception) {
        NSLog(@"Write Exception:%@",exception.reason);
        return false;
    }
}
+(id)readFromPropertyListFile:(NSString *)fileName fromKey:(NSString *)keyName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSString *path = [libraryDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.plist",fileName] ];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    return [data objectForKey:keyName];
}
+(NSMutableDictionary*)readFromPropertyListFile:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSString *path = [libraryDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.plist",fileName] ];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    return data;
}
+(NSMutableArray*)loadFromJSONfile:(NSString *)fileName
{
    @try
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *libraryDirectory = [paths objectAtIndex:0];
        NSString *path = [libraryDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@",fileName]];
        NSString *strJSON=[[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSDictionary *dic=[strJSON objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        NSMutableArray *array=[dic objectForKey:@"items"];
        return array;
    }
    @catch (NSException *exception)
    {
        return [[NSMutableArray alloc]initWithObjects:exception.reason, nil];
    }
}

+(NSDictionary*)loadFromJSONfiletoDictionary:(NSString *)fileName
{
    @try
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *libraryDirectory = [paths objectAtIndex:0];
        NSString *path = [libraryDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.json",fileName]];
        NSString *strJSON=[[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSDictionary *dic=[strJSON objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        return dic;
    }
    @catch (NSException *exception)
    {
        return [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObject:@"Exception"] forKeys:[NSArray arrayWithObject:@"exception.reason"]];
        
    }
}

+(BOOL)isMobileNumber:(NSString*)mobileNum
{
    NSString* MOBILE=@"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    NSString* CM=@"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    NSString* CU=@"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    NSString* CT=@"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    NSPredicate *regextestmobile= [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MOBILE];
    NSPredicate *regextestcm= [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CM];
    NSPredicate *regextestcu= [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CU];
    NSPredicate *regextestct= [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CT];
    if(([regextestmobile evaluateWithObject:mobileNum]== YES)||([regextestcm evaluateWithObject:mobileNum]== YES)||([regextestct evaluateWithObject:mobileNum]== YES)||([regextestcu evaluateWithObject:mobileNum]== YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+(void)removeFilesWithNames:(NSArray *)fileNames
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSArray* strPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* strDocumentDirectory=[strPaths objectAtIndex:0];
    NSString* strFilePath;
    for (int k=0; k<fileNames.count; k++)
    {
        strFilePath =[strDocumentDirectory stringByAppendingPathComponent:[fileNames objectAtIndex:k]];
        [fileManager removeItemAtPath:strFilePath error:nil];
    }
}

+(BOOL)removeDownloadedImages{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSArray* strPaths =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString* strImagesDirectory=[strPaths objectAtIndex:0];
    strImagesDirectory=[strImagesDirectory stringByAppendingPathComponent:@"Caches/com.almas.Yurnal.aiimages"];
    NSArray* imageList = [[[NSFileManager alloc] init]
                  contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/com.almas.Yurnal.aiimages"]
                  error:nil];
    NSString* strFilePath;
    for (int k=0; k<imageList.count; k++)
    {
        strFilePath =[strImagesDirectory stringByAppendingPathComponent:[imageList objectAtIndex:k]];
        if (![fileManager removeItemAtPath:strFilePath error:nil]) {
            return NO;
        }
    }
    return YES;
}

+(BOOL)fileExistWithName:(NSString *)strFileName
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSArray* strPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* strDocumentDirectory=[strPaths objectAtIndex:0];
    NSString* strFilePath =[strDocumentDirectory stringByAppendingPathComponent:strFileName];
    return [fileManager fileExistsAtPath:strFilePath];

}

+(BOOL)fileExistAtPath:(NSString *)strFilePath
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    return [fileManager fileExistsAtPath:strFilePath];
}

+(NSString*)getDocumentdirettory{
    NSArray* strPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [strPaths objectAtIndex:0];
}

@end
